/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webservices;

import entities.ArtistType;
import fm.last.musicbrainz.coverart.CoverArt;
import fm.last.musicbrainz.coverart.CoverArtArchiveClient;
import fm.last.musicbrainz.coverart.CoverArtImage;
import fm.last.musicbrainz.coverart.impl.DefaultCoverArtArchiveClient;
import init.Config;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import org.musicbrainz.model.entity.ArtistWs2;
import org.musicbrainz.model.entity.ReleaseWs2;
import org.musicbrainz.model.searchresult.ArtistResultWs2;
import org.musicbrainz.model.searchresult.ReleaseResultWs2;
import util.FileCopyUtil;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
public class MusicWebServices {
    // Get the album cover using Covert Art Archive Web services
    // the image is saved in the cover art directory and the relative path is returned

    public static String getCoverArt(String albumTitle) {
        // Get the album UUID from the MusicBrainz web service
        String relativePathArt = Config.DEFAULT_COVER_ART_FILE;

        if (albumTitle != null && !albumTitle.equals("")) {
            org.musicbrainz.controller.Release mbRelease = new org.musicbrainz.controller.Release();
            mbRelease.search(albumTitle);
            List<ReleaseResultWs2> results;
            results = mbRelease.getFirstSearchResultPage();

            if (results.size() > 0) {
                ReleaseResultWs2 aw2 = results.get(0); // Get the first result
                ReleaseWs2 releaseWs2 = aw2.getRelease();
                String mbidStr = releaseWs2.getId();

                // Get the cover using the Cover Art Archive web service
                CoverArtArchiveClient client = new DefaultCoverArtArchiveClient();
                UUID mbid = UUID.fromString(mbidStr);

                try {
                    CoverArt coverArt = client.getByMbid(mbid);
                    if (coverArt != null) {
                        CoverArtImage coverArtImage = coverArt.getImages().get(0);
                        coverArtImage.getImage();

                        // Create a jpeg file in the web directory
                        String filePath = Config.SERVER_WEB_DIR + File.separator + Config.COVER_ART_DIRECTORY + File.separator + mbid.toString() + "_" + coverArtImage.getId() + ".jpg";
                        relativePathArt = filePath.substring(Config.SERVER_WEB_DIR.length() + 1, filePath.length());
                        File outputFile = new File(filePath);
                        OutputStream output = new FileOutputStream(outputFile);
                        FileCopyUtil.copyInputstreamToFile(coverArtImage.getImage(), output);
                    }
                } catch (Exception e) {
                }
            }
        }

        return relativePathArt.replaceAll("\\\\", "/");
    }

    // Get informations about an artist via Music Brainz Web services
    public static ArtistResult getArtistInfos(String artistName) {
        // WEB SERVICE using MusicBrainz
        if (artistName != null && !artistName.equals("")) {
            org.musicbrainz.controller.Artist mbArtist = new org.musicbrainz.controller.Artist();
            mbArtist.search(artistName);
            List<ArtistResultWs2> results;
            results = mbArtist.getFirstSearchResultPage();

            int beginningYear = 2000;
            ArtistType artistType = ArtistType.SOLO;
            if (results.size() > 0) {
                ArtistResultWs2 aw2 = results.get(0); // Get the first result
                ArtistWs2 artistWs2 = aw2.getArtist();

                String artistTypeStr = artistWs2.getType();
                String beginingDateStr = artistWs2.getLifeSpan().getBegin();

                // Artist type
                if (artistTypeStr != null && !artistTypeStr.equals("") && artistTypeStr.equals("http://musicbrainz.org/ns/mmd-2.0#Group")) {
                    artistType = ArtistType.GROUP;
                }

                // Beginning year
                if (beginingDateStr != null) {
                    if (beginingDateStr.length() == 10) {
                        beginningYear = Integer.valueOf(beginingDateStr.substring(0, 3));
                    } else if (beginingDateStr.length() == 4) {
                        beginningYear = Integer.valueOf(beginingDateStr);
                    }
                }
            }

            return new ArtistResult(beginningYear, artistType);
        } else {
            return null;
        }
    }
}
